//--服务中心--
const app = getApp()

var timestamp = Date.parse(new Date());
var date = new Date(timestamp), rulerv;///rulerv控制权限
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgalist: ['https://7a6d-zmyy-6uke0-1301213777.tcb.qcloud.la/%E9%9C%8D%E5%B8%82%E5%9C%A8%E8%A1%8C%E5%8A%A8.jpg?sign=fbacfc3839973fcd2d1c2eb107a5c4fa&t=1580886350']
  },
  onGetOpenid: function () {
    const db = wx.cloud.database();
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        app.globalData.openid = res.result.openid
        //console.log(res.result.openid)
        db.collection('glry').where({
          _openid: app.globalData.openid,
        })
          .get()
          .then(res => {
            // console.log('查询权限res.data', res.data)
            if (res.data.length) {
              // console.log('有权限')
                    if (res.data[0].ruler) {
                      // console.log('ruler有权限')
                      rulerv = true
                    }
                    else {
                      // console.log('ruler没有权限')
                    }            
            }
            else {
              // console.log('没有权限')
            }
           
          })
      },

    })
  },
  imageLoad: function (e) {
    var width1 = e.detail.width,    //获取图片真实宽度
      height1 = e.detail.height,
      ratio = width1/height1    ///图片的真实宽高比例
    var viewWidth = 50,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 1000/ ratio;    ///计算的高度值
    var image = this.data.imgalist;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[e.target.dataset.index] = {
      width: 50,
      height: 100
    }
    this.setData({
      imgalist: image
    })
  },
  onLoad: function () {
    this.onGetOpenid();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

  },
  onAdd: function (e) {
    const db = wx.cloud.database();

    // console.log(e.detail.userInfo)
    db.collection('phone').where({
      _openid: app.globalData.openid
    })
      .get()
      .then(res => {
        if (res.data.length) {
          wx.showToast({
            title: '已添加过'
          })
        }
        else {
          //console.log('没添加过电话', res.data)           
          wx.navigateTo({
            url: '/pages/addPhone/addPhone'
          })
        }
      })
    db.collection('phone').where({
      _openid: app.globalData.openid
    })
      .get(
        {
          success: res => {
            this.setData({
              vw: true,
              qn: res.data[0].name,
              qp: res.data[0].phone
            })
            //console.log('[数据库] [查询记录] 成功: ', res.data)              
          }
        }
      )


  },
	/** 
	 * 预览图片
	 */
  PersonInfo: function (e) {
    wx.navigateTo({
      url: '../PersonInfo/PersonInfo',
    })
  },
  gzryInfo: function (e) {
    wx.navigateTo({
      url: '../gzryinfo/gzryinfo',
    })
  },
  myReported: function (e) {
    wx.navigateTo({
      url: '../myReported/myReported',
    })
  },
  PersonReported: function (e) {
    wx.navigateTo({
      url: '../PersonReported/PersonReported',
    })
  },
  zhuanzhen: function (e) {
    // console.log('[数据库] [查询记录] 成功: ', rulerv)  
    if (rulerv) {
    wx.navigateTo({
      url: '../PersonReported/zhuanzhen_tracer/zhuanzhen_tracer',
      })
    } else {
      wx.showToast({
        title: '没有权限进入',
        icon: 'success',
        // duration: 2000
      })
    }
  },
  datadown: function (e) {
    wx.navigateTo({
      url: '../dataDown/dataDown',
    })
  },
  tool: function (e) {
    wx.navigateTo({
      url: '../tool/tool',
    })
  },
  about: function (e) {
    wx.navigateTo({
      url: '../about/about',
    })
  },
})
